from django.contrib.auth.backends import ModelBackend
from .models import UserModel
import logging
logger = logging.getLogger('erp')


class UserLoginAuth(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        """
        实现用户登陆
        """
        try:
            user = UserModel.objects.get(username=username)
            logger.info(user.username)
        except:
            return None
        # 如果数据库中有这个密码，我们直接判断
        if user.check_password(password):
            return user  # 把user对象放到request对象中去
